﻿

var server = (function () {
    // connection with the sever
    // retrieving menu items
    // sending orders
    // user log-in
    // and so on...
    //This object contains variables. These variables contain the names, which should be used
    //everytime, we save something
    var nameConstants = {
        menuItems: "menuItems",
        order: "order",
        ordersHistoryLSKey: "ordersHistory"
    };

    

    function loadData(dataObjectName) {
        if (typeof dataObjectName === 'undefined' || dataObjectName === '') {
            throw new TypeError("dataObjectName is not specified");
        }
        var dataObject = localStorage.getObject(dataObjectName);
        //dataObject = JSON.parse(dataObject);
        if (dataObject === null) { //If the name of the object is incorrect nothing will be loaded
            //dataObject will be null
            throw new TypeError("dataObject was not loaded correctly");
        }
        return dataObject;
    }

    function saveData(dataObjectName, dataArray) {
        if (typeof dataObjectName === 'undefined' || dataObjectName === '') {
            throw new TypeError("dataObjectName is not specified");
        } else if (typeof dataArray === 'undefined' || dataArray === null) {
            throw new TypeError("dataArray is not specified");
        } else {
            //localStorage.setItem(dataObjectName, JSON.stringify(dataArray));
            localStorage.setObject(dataObjectName, dataArray);
        }
    }

    //utility function

    function prepareMenu() {
        //localStorage.clear();
        var menuJSON = [{
                categoryName: "Chiken meat",
                data: [{
                        id: 20001,
                        name: "Chicken Corn Flakes",
                        price: 66.59,
                        time: 6,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/ChickenCornFlakes.jpg"
                    }, {
                        id: 20002,
                        name: "Chicken Steak",
                        price: 4.59,
                        time: 7,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/ChickenSteak.jpg"
                    }, {
                        id: 20003,
                        name: "Chicken bon fillet.jpg",
                        price: 7.59,
                        time: 8,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/Chickenbon fillet.jpg"
                    }, {
                        id: 20004,
                        name: "Hunter's Chicken Recipe",
                        price: 9.59,
                        time: 9,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/ChikenHunting.JPG"
                    }, {
                        id: 20005,
                        name: "Chiken Julienne",
                        price: 10.59,
                        time: 12,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/ChkikenJulien.JPG"
                    }
                ]
            }, {
                categoryName: "Salads",
                data: [{
                        id: 30001,
                        name: "Shepherd slad",
                        price: 3.59,
                        time: 7,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/Shepherd.jpg"
                    }, {
                        id: 30002,
                        name: "Shop salad",
                        price: 5.59,
                        time: 1,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/Shop.jpg"
                    }, {
                        id: 30003,
                        name: "Chinese salad",
                        price: 6.59,
                        time: 2,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/chinese.jpg"
                    }, {
                        id: 30004,
                        name: "Caesar salad",
                        price: 8.59,
                        time: 42,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/caesar-salad.jpg"
                    }, {
                        id: 30005,
                        name: "Green salad",
                        price: 14.59,
                        time: 43,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/green-salad.jpg"
                    }
                ]
            }, {
                categoryName: "Soups",
                data: [{
                        id: 40001,
                        name: "Chicken soup",
                        price: 3.59,
                        time: 7,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/chickensoup.jpg"
                    }, {
                        id: 40002,
                        name: "Bean soup",
                        price: 5.59,
                        time: 1,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/BeanSoup.jpg"
                    }, {
                        id: 40003,
                        name: "Pork soup",
                        price: 6.59,
                        time: 2,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/Pork Soup.jpg"
                    }, {
                        id: 40004,
                        name: "Mushroom soup",
                        price: 8.59,
                        time: 42,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/MushroomSoup.jpg"
                    }, {
                        id: 40005,
                        name: "French onion soup",
                        price: 14.59,
                        time: 43,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/FrenchOnionSoup.jpg"
                    }
                ]
            },
            {
                categoryName: "Pizzas",
                data: [
                    {
                      id: 50001,
                        name: "Capricciosa",
                        price: 3.59,
                        time: 7,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/capricciosa.jpg"
                    }, {
                        id: 50002,
                        name: "Oskosh",
                        price: 5.59,
                        time: 1,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/Oskosh.jpg"
                    }, {
                        id: 50003,
                        name: "Venezia",
                        price: 6.59,
                        time: 2,
                        description: "some text goes here",
                        isPromotion: true,
                        imgSrc: "img/Venezia.jpg"
                    }, {
                        id: 50004,
                        name: "Margarita",
                        price: 8.59,
                        time: 42,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/Margarita.jpg"
                    }, {
                        id: 50005,
                        name: "Vegetariana",
                        price: 14.59,
                        time: 43,
                        description: "some text goes here",
                        isPromotion: false,
                        imgSrc: "img/Vegetariana.jpg"
                    }
                ]
            }
        ];

        saveData(nameConstants.menuItems, menuJSON);
    }

    //loads menu from storag

    function getMenu() {
        ///////////////////
        prepareMenu(); //clears local storage and saves menu, used for testing
        //////////////////
        var menuJSON = loadData(nameConstants.menuItems);
        return menuJSON;
    }

    function sendOrder(order) {
        try {
            saveData(nameConstants.order, order);
        }
        catch (e) {
            console.log(e.name + ": " + e.message);
        }
    }

    function saveOrder(orderData) {
        var ordersHistory = localStorage.getObject(nameConstants.ordersHistoryLSKey);
        if (ordersHistory === null) {
            ordersHistory = [];
        }
        ordersHistory.push(orderData);
        localStorage.setObject(nameConstants.ordersHistoryLSKey, ordersHistory);

        return ordersHistory;
    }

    function clearHistory() {
        
        localStorage.setObject(nameConstants.ordersHistoryLSKey, []);
    }

    function getOrdersHistory() {
        var ordersHistory = localStorage.getObject(nameConstants.ordersHistoryLSKey);
        return ordersHistory;
    }

    return {
        getMenu: getMenu,
        saveData: saveData,
        loadData: loadData,
        saveOrder: saveOrder,
        getOrdersHistory: getOrdersHistory,
        sendOrder: sendOrder,
        clearHistory: clearHistory
    };
})();